var express = require('express');
var md5 = require('md5');
var request = require('request');
var consts = require('../common/constants');

var router = express.Router();

//收货地址列表
router.get('/list', function(req, res, next) {
	var params = {
		userId: req.query.uid
	};

	var uid = req.query.uid;
	var token = JSON.stringify(params) + "|" + consts.MD5_String;
	token = md5(token);
	var url = consts.APP_HOST + 'address/list.htm?token=' + token;
	var options = {
		url: url,
		method: 'POST',
		json: true,
		body: params
	};

	var change = req.query.change;

	if (change == 'add') {
		var message = '1';
	} else if (change == 'true') {
		var message = '0';
	} else {
		var message = '2';
	}
	request(options, function(err, httpResponse, body) {
		console.log(body);
		if (1 == body.MobileHead.Code) {
			//res.send(body.MobileBody);

			res.render('userAdd', {

				addressList: body.MobileBody,
				uid: uid
			})

		} else {
			res.send('404');
		}
	});
});

//新增地址
router.get('/addnew', function(req, res, next) {
	var params = {
		userId: req.query.uid
	};

	var uid = req.query.uid;
	var token = JSON.stringify(params) + "|" + consts.MD5_String;
	token = md5(token);
	var url = consts.APP_HOST + 'address/list.htm?token=' + token;
	var options = {
		url: url,
		method: 'POST',
		json: true,
		body: params
	};

	request(options, function(err, httpResponse, body) {

		if (1 == body.MobileHead.Code) {
			console.log(body.MobileBody);
			res.render('userNewadd', {
				info: body.MobileBody,
				uid: uid
			})
		} else {
			res.send('404');
		}

	});
});

//修改地址静态
router.get('/addchange', function(req, res, next) {
	var params = {
		userId: req.query.uid,
		addressId: req.query.aid
	};

	var uid = req.query.uid;
	var aid = req.query.aid;
	var token = JSON.stringify(params) + "|" + consts.MD5_String;
	token = md5(token);
	var url = consts.APP_HOST + 'address/list.htm?token=' + token;
	var options = {
		url: url,
		method: 'POST',
		json: true,
		body: params
	};

	request(options, function(err, httpResponse, body) {

		if (1 == body.MobileHead.Code) {
			console.log(body.MobileBody);
			res.render('userChangeadd', {
				info: body.MobileBody,
				uid: uid,
				aid: aid,

			})
		} else {
			res.send('404');
		}

	});
});

//新增收货地址
router.post('/add', function(req, res, next) {
	var params = {
		userId: req.body.uid,
		name: req.body.name,
		address: req.body.address,
		provinceCode: req.body.provinceCode,
		cityCode: req.body.cityCode,
		areaCode: req.body.areaCode,
		mobile: req.body.mobile,
		isDefault: req.body.isDefault,
		pcaDetail: req.body.pcaDetail
	};
	var uid = req.body.uid;
	var token = JSON.stringify(params) + "|" + consts.MD5_String;
	token = md5(token);
	var url = consts.APP_HOST + 'address/add.htm?token=' + token;
	var options = {
		url: url,
		method: 'POST',
		json: true,
		body: params
	};

	request(options, function(err, httpResponse, body) {

		console.log(body)

		if (1 == body.MobileHead.Code) {

			res.send('200');

		} else {
			res.send('404');

		}
	});

});

//编辑收货地址
router.post('/edit', function(req, res, next) {
	var params = {
		id: req.body.aid,
		userId: req.body.uid,
		name: req.body.name,
		address: req.body.address,
		provinceCode: req.body.provinceCode,
		cityCode: req.body.cityCode,
		areaCode: req.body.areaCode,
		mobile: req.body.mobile,
		isDefault: req.body.isDefault,
		pcaDetail: req.body.pcaDetail
	};

	var uid = req.body.uid;
	var token = JSON.stringify(params) + "|" + consts.MD5_String;
	token = md5(token);
	var url = consts.APP_HOST + 'address/add.htm?token=' + token;
	var options = {
		url: url,
		method: 'POST',
		json: true,
		body: params
	};

	request(options, function(err, httpResponse, body) {
		console.log(body);
		if (1 == body.MobileHead.Code) {

			res.send('200');
		} else {
			res.send('404');
		}
	});
});

//删除收货地址
router.post('/delete', function(req, res, next) {
	var params = {
		id: req.body.id
	};
	var token = JSON.stringify(params) + "|" + consts.MD5_String;
	token = md5(token);
	var url = consts.APP_HOST + 'address/delete.htm?token=' + token;
	var options = {
		url: url,
		method: 'POST',
		json: true,
		body: params
	};

	request(options, function(err, httpResponse, body) {
		console.log(body);
		if (1 == body.MobileHead.Code) {
			res.send('200');
		} else {
			res.send('404');
		}
	});
});

//设置默认收货地址
router.post('/default', function(req, res, next) {
	var params = {
		id: req.body.aid
	};
	var token = JSON.stringify(params) + "|" + consts.MD5_String;
	token = md5(token);
	var url = consts.APP_HOST + 'address/default.htm?token=' + token;
	var options = {
		url: url,
		method: 'POST',
		json: true,
		body: params
	};

	request(options, function(err, httpResponse, body) {
		console.log(body);
		if (1 == body.MobileHead.Code) {
			res.send('200');
		} else {
			res.send('404');
		}
	});
});

module.exports = router;